package com.aqielife.demo.service.impl;


import com.aqielife.demo.entity.City;
import com.aqielife.demo.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
import org.springframework.data.cassandra.core.query.Query;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.springframework.data.cassandra.core.query.Criteria.where;

/**
 * @author aqie
 * @date 2022/02/05 18:44
 * @desc
 */
@Slf4j
@Service("cityServiceCassandraImpl")
public class CityServiceCassandraImpl implements CityService {
  @Autowired
  private ReactiveCassandraTemplate cassandraTemplate;

  @Override
  public Mono<City> save(City city) {
    return cassandraTemplate.insert(city);
  }

  @Override
  public Mono<City> findCityById(String id) {
    return cassandraTemplate.selectOneById(id, City.class);
  }

  @Override
  public Flux<City> findAllCity() {
    return cassandraTemplate.select("SELECT * FROM city ;", City.class);
  }

  @Override
  public Mono<City> modifyCity(City city) {
    return cassandraTemplate.update(city);
  }

  @Override
  public void deleteCity(String id) {
    Mono<Boolean> booleanMono = cassandraTemplate.delete(Query.query(where("id").is(id)), City.class);
    booleanMono.subscribe(e-> {
      System.out.println("result " + e);
    });
  }

  @Override
  public String getCityInfo(){
    try {
      Thread.sleep(100);//模拟调用耗时
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "hebei";
  }

  @Override
  public String getCityInfoHistory(String city){
    try {
      Thread.sleep(200);//模拟调用耗时
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return city + "-History";
  }

  @Override
  public String getCityMedal(){
    try {
      Thread.sleep(300); //模拟调用耗时
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "CityMedal";
  }
}
